import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/admin', // 配置统一前缀
    name: 'home',
    component: HomeView,
    // redirect: '/admin/user/list', // 重定向
    children:[
      {
        // 不以 / 作为第一个字符，表示相对路径
        path:'user/add-new',
        component: ()=>import("../views/user/UserAddNewView.vue"),
        meta:{
          title: '添加用户'
        }
      },
      {
        path:'user/list',
        component: ()=>import("../views/user/UserListView.vue"),
        meta:{
          title: '用户列表'
        }
      },
      {
        path:'brand/list',
        component: ()=>import("../views/brand/BrandListView.vue"),
        meta:{
          title: '品牌列表'
        }
      },
      {
        path:'brand/add-new',
        component: ()=>import("../views/brand/BrandAddNewView.vue"),
        meta:{
          title: '添加品牌'
        }
      },
      {
        path:'category/add-new',
        component: ()=>import("../views/category/CategoryAddNewView.vue"),
        meta:{
          title: '添加类别'
        }
      },
      {
        path:'admin/add-new',
        component: ()=>import("../views/admin/AdminAddNewView.vue"),
        meta:{
          title: '添加管理员'
        }
      },
      {
        path:'admin/list',
        component: ()=>import("../views/admin/AdminListView.vue"),
        meta:{
          title: '管理员列表'
        }
      }
    ]
  },
  {
    path:'/',
    component: () => import('../views/LoginView.vue')
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router


router.beforeEach((to, from, next)=>{

  if(to.meta.title){
    document.title = to.meta.title;
  }

  next(); // "放行"
})

